﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.Common;
using SharpDiary.Core.Controls;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;

namespace SharpDiary.SettingsManager
{
    [GuiModule("b7dd01d2-bd49-40cc-9ccb-7690980945e0", "SettingsManager", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmSettingsManager : GuiModuleUIBase
    {
        #region Fields & Properties

        private Control _activeSettingsControl;

        /// <summary>
        /// 当前设置组件。
        /// </summary>
        public Control ActiveSettingsControl
        {
            get { return _activeSettingsControl; }
            set { _activeSettingsControl = value; }
        }

        #endregion

        #region Constructors

        public frmSettingsManager()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            ShowSettingsItems();
            if (lsbSettingsNames.Items.Count > 0) { lsbSettingsNames.SelectedIndex = 0; }
        }

        /// <summary>
        /// 显示设置项。
        /// </summary>
        private void ShowSettingsItems()
        {
            lsbSettingsNames.Items.Clear();

            List<ModuleSettingsInfo> settings = ModuleSettingsInfo.GetAllModuleSettingsInfo();
            foreach (ModuleSettingsInfo setting in settings)
            {
                ListBoxItemEx item = new ListBoxItemEx();
                item.SettingsInfo = setting;
                item.SettingsControl = null;

                lsbSettingsNames.Items.Add(item);
            }
        }

        /// <summary>
        /// 显示模块设置。
        /// </summary>
        /// <param name="settingsInfo"></param>
        private void ShowSettingsUI(ListBoxItemEx item)
        {
            if (item == null) { return; }

            if (item.SettingsControl == null)
            {
                if (_activeSettingsControl != null) { _activeSettingsControl.Hide(); }

                ISettingsUI instance = UserSettings.ShellInstance.ModuleManager.GetSettingsInstance(item.SettingsInfo.AssemblyString, item.SettingsInfo.Id);
                if (instance == null) { return; }

                SettingsControlBase control = instance.GetUIInstance();
                item.SettingsControl = control;
                control.Dock = DockStyle.Fill;

                pnlSettingsControlContainer.Controls.Add(control);

                if (_activeSettingsControl != null) { _activeSettingsControl.Hide(); }
                control.Show();
                control.BringToFront();

                //加载配置信息。
                control.LoadSettingsInfo();
            }
            else
            {
                if (_activeSettingsControl != null) { _activeSettingsControl.Hide(); }
                _activeSettingsControl = item.SettingsControl;

                item.SettingsControl.Show();
                item.SettingsControl.BringToFront();
            }
        }

        /// <summary>
        /// 保存设置信息。
        /// </summary>
        private bool Save()
        {
            foreach (object obj in lsbSettingsNames.Items)
            {
                ListBoxItemEx item = obj as ListBoxItemEx;
                if (item == null) { continue; }

                SettingsControlBase control = item.SettingsControl as SettingsControlBase;
                if (control == null) { continue; }

                string errorMsg = string.Empty;
                if (!control.CheckIfInputOk(ref errorMsg))
                {
                    popupMessageControl.Show(errorMsg);

                    lsbSettingsNames.SelectedItem = item;

                    if (_activeSettingsControl != null) { _activeSettingsControl.Hide(); }
                    control.Show();
                    control.BringToFront();

                    return false;
                }
                else
                {
                    if (!control.SaveSettingsInfo())
                    {
                        popupMessageControl.Show(item.SettingsInfo.Name + ": 保存失败。");
                        return false;
                    }
                }
            }

            return true;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                popupMessageControl.Show("保存成功。");
                DelayClose(1000);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmSettingsManager_Shown(object sender, EventArgs e)
        {
            diarySettingsControl.LoadSettingsInfo();
        }

        #endregion

        private void lsbSettingsNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBoxItemEx item = lsbSettingsNames.SelectedItem as ListBoxItemEx;
            if (item == null) { return; }

            ShowSettingsUI(item);
        }

        private void frmSettingsManager_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.S: //Ctrl+S: 保存
                        {
                            btnOk.PerformClick();
                            break;
                        }
                }
            }
        }
    }

    internal class ListBoxItemEx
    {
        #region Fields & Properties

        private ModuleSettingsInfo _settingsInfo;

        public ModuleSettingsInfo SettingsInfo
        {
            get { return _settingsInfo; }
            set { _settingsInfo = value; }
        }

        private Control _settingsControl;

        public Control SettingsControl
        {
            get { return _settingsControl; }
            set { _settingsControl = value; }
        }

        #endregion

        #region Constructors

        public ListBoxItemEx() { }

        #endregion

        #region Methods

        public override string ToString()
        {
            return (_settingsInfo != null ? _settingsInfo.Name : string.Empty);
        }

        #endregion
    }
}
